;(function() {
	'use strict';
	angular.module('controllers.faultLocation.alarmStatisticAnalysis')
		.controller('appAlarmCtrl', appAlarmCtrl);

	function appAlarmCtrl($scope, appAlarmServerData, System, CoreService, appAlarmServer,$timeout,CoreLoading) {
		$scope.shoumun = {
			num: true
		};
		$scope.sendData = appAlarmServer.getPageBind();
		/**
		 *@function [initQuery] 初始化查询，给系统名称设置默认值，并查询告警级别和状态
		 */
		$scope.initQuery = function() {
			var sys = {
				systemCode: $scope.sendData.systemCode
			};
			appAlarmServer.queryAlarmLevel(sys, function(result) {
				if (result.state === '10000') {
					$scope.levels = result.data;
				} else {
					CoreService.dialog('当前查询无数据');
				}
			});
			appAlarmServer.queryAlarmState(sys, function(result) {
				if (result.state === '10000') {
					$scope.states = result.data;
				} else {
					CoreService.dialog('当前查询无数据');
				}
			});
		};

		/**
		 * @function [sysCascade] 单击系统名称，级联告警级别和状态
		 * @param  {[Object]} system 系统名称对象，根据不同的systemCode，查询不同的告警级别和状态
		 * @return {[type]}
		 */
		$scope.sysCascade = function(systemCode) {
			var sys = {
				systemCode: systemCode
			};
			appAlarmServer.queryAlarmLevel(sys, function(result) {
				if (result.state === '10000') {
					$scope.levels = result.data;
				} else {
					CoreService.dialog('当前查询无数据');
				}
			});
			appAlarmServer.queryAlarmState(sys, function(result) {
				if (result.state === '10000') {
					$scope.states = result.data;
				} else {
					CoreService.dialog('当前查询无数据');
				}
			});
		};
		$scope.send = {
			page: {
				currentPage: 1,
				pageSize: '10'
			}
		};
		$scope.search = function() {
			if ($scope.shoumun.num === true || $scope.myForm.$invalid) {
				$scope.shoumun.num = true;
				return;
			}
			//时间粒度为天时，最多只能查询31天的数据
			var endTime = $scope.sendData.endTime.getTime();
			var startTime = $scope.sendData.startTime.getTime();
			var day = (endTime - startTime) / 1000 / 60 / 60 / 24;
			var hour = parseInt((endTime - startTime) / 1000 / 60 / 60);
			/*if ($scope.sendData.timeType === '1') {*/
			if (day > 30) {
				$scope.shoumun.num = true;
				CoreService.dialog('粒度为天时,只能查询31天内的数据！');
				return;
			}

			/*} else {
				if (hour > 47) {
					$scope.shoumun.num = true;
					CoreService.dialog('粒度为小时时,只能查询48小时内的数据！');
					return;
				}
			}*/
			$scope.send = appAlarmServer.fmtSendData($scope.sendData, $scope.levels, $scope.states);
			/*if ($scope.sendData.msg !== '') {
				$scope.shoumun.num = true;
				CoreService.dialog('当前查询无数据');
				return;
			}*/
			CoreLoading.setState(true);
			$timeout(function(){
				appAlarmServer.queryAppAlarmNum($scope.send);
			});
			$scope.flag = 'view';
		};
		//切换表格
		$scope.changeTable = function() {
			CoreLoading.setState(true);
			//debugger;
			appAlarmServer.queryAppAlarmNumTable($scope.send, function(res) {
				CoreLoading.setState(false);
				if (res.state === '10000') {
					var temp = {
						echartsXData: [],
						level: '',
						state: ''
					};
					angular.forEach(res.data.data, function(item) {
						temp.level = item.alarmLevelName;
						temp.state = item.alarmStateName;
						if (temp.echartsXData.indexOf(item.time)) {
							temp.echartsXData.push(item.time);
						}
					});
					$scope.items = appAlarmServer.ypaging($scope.send, res.data);
					var title = '的应用告警数统计';
					if (temp.echartsXData[0] === temp.echartsXData[temp.echartsXData.length - 1]) {
						$scope.tableTitle = temp.echartsXData[0].replace(/-/g, '') + title;
					} else {
						$scope.tableTitle = temp.echartsXData[0].replace(/-/g, '') + ' 到 ' + temp.echartsXData[temp.echartsXData.length - 1].replace(/-/g, '') + title;
					}
					$scope.flag = 'table';
				} else {
					CoreService.dialog('查询错误');
				}
			});

		};
		//切换图形
		$scope.changeView = function() {
			$scope.flag = 'view';
		};
		$scope.sendDet = {
			page: {
				currentPage: 1,
				pageSize: 10
			}

		};
		//表格下钻
		$scope.tableDown = function(item) {
			//debugger;
			CoreLoading.setState(true);
			$scope.sendDet.alarmLevelCode = $scope.send.alarmLevelCode;
			$scope.sendDet.alarmLevelName = $scope.send.alarmLevelName;
			$scope.sendDet.alarmStateCode = $scope.send.alarmStateCode;
			$scope.sendDet.alarmStateName = $scope.send.alarmStateName;
			$scope.sendDet.systemCode = $scope.send.systemCode;
			$scope.sendDet.timeType = $scope.send.timeType;
			$scope.sendDet.time = item.time;
			$scope.sendDet.alarmNum = item.alarmNum;
			/*$scope.sendDet.page = {
				currentPage: 1,
				pageSize: '10'
			};*/
			//总行数
			$scope.totalCount = item.alarmNum;
			$scope.titleDetTable = item.time.replace(/-/g, '') + '应用告警数详细统计';
			$scope.downItmes = appAlarmServer.queryDetAlarmNums($scope.sendDet);
			//console.log($scope.downItmes.flag);


			$scope.flag = 'tableDown';
		};
		$scope.back = function() {
			$scope.flag = 'table';
		};
	}
	appAlarmCtrl.$inject = ['$scope','appAlarmServerData', 'System', 'CoreService', 'appAlarmServer','$timeout','CoreLoading'];
})();